package userInterface;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import search.*;
import au.com.bytecode.opencsv.*;

/**
 ** A simple user interface for SEPT Assignment 3.
 **/

/**
 * @author Ryan
 * @start_date 2012/04/03
 */

public class TempInterface extends JFrame {
	public static void main(String[] args) {
		new UIPanelRightNorth();
		// Create a new object (instance) of our JFrame subclass.
		UIFrame window = new UIFrame();
		// Search search = new Search();
		// ArrayList<String> searchResult = search.doSearch(args[0], args[1],
		// args[2], args[3], args[4], args[5], args[6], args[7], args[8],
		// args[9]);
		// Iterator<String> searchResultIterator = searchResult.iterator();
		// while (searchResultIterator.hasNext()) {
		// System.out.println(searchResultIterator.next());
		// }
		// Centre the window.
		window.setLocationRelativeTo(null);
		// Terminate the program.
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// Display the window. Make it visible.
		window.setVisible(true);
	}
}

class UIFrame extends JFrame {
	public UIFrame() {
		setTitle("SEPT Assignment 3 - Group VC");
		// Window width in pixels.
		int windowWidth = 1350;
		// Window height in pixels.
		int windowHeight = 500;
		// Set window position and size.
		setBounds(0, 0, windowWidth, windowHeight);
		UIPanelNorth panelNorth = new UIPanelNorth();
		UIPanelCenter panelCenter = new UIPanelCenter();
		UIPanelSouth panelSouth = new UIPanelSouth();
		Container c = getContentPane();
		c.add(panelNorth, BorderLayout.NORTH);
		c.add(panelCenter, BorderLayout.CENTER);
		c.add(panelSouth, BorderLayout.SOUTH);
	}
}

class UIPanelNorth extends JPanel {
	UIPanelLeftNorth panelLeftNorth = new UIPanelLeftNorth();
	UIPanelRightNorth panelRightNorth = new UIPanelRightNorth();

	public UIPanelNorth() {
		add(panelLeftNorth, BorderLayout.WEST);
		add(panelRightNorth, BorderLayout.EAST);
		// Set flow layout, aligned right with horizontal gap 50 and vertical
		// gap 30 between components.
		setLayout(new FlowLayout(FlowLayout.CENTER, 50, 30));
	}
}

class UIPanelLeftNorth extends JPanel {
	public UIPanelLeftNorth() {
		// Set grid layout, 4 rows, 0 columns, horizontal gap 0 and vertical gap
		// 15 between components.
		setLayout(new GridLayout(4, 0, 0, 15));
		// Create labels to the UIPanelLeftNorth.
		JLabel fromLabel = new JLabel("From: ");
		JLabel toLabel = new JLabel("To: ");
		JLabel departingDateLabel = new JLabel("Departing Date: ");
		JLabel departingTimeLabel = new JLabel("Departing Time: ");
		// Add labels to the UIPanelLeftNorth.
		add(fromLabel);
		add(toLabel);
		add(departingDateLabel);
		add(departingTimeLabel);
	}
}

class UIPanelRightNorth extends JPanel {
	int checkError;
	// ComboBox of Depart Service.
	private String[] departService = { "(Service)", "Train", "Tram", "Bus" };
	// ComboBox of Depart Line.
	private String[] departLine = { "(Line)" };
	// ComboBox of departing Train line.
	private String[] departTrainLine = { "(Line)", "Belgrave", "Lilydale" };
	// ComboBox of departing Tram line.
	private String[] departTramLine = { "(Line)", "109" };
	// ComboBox of Depart Station Default.
	private String[] departStationDefault = { "(Station/Stop)" };
	// ComboBox of Depart Station.
	private String[] departStation = {};
	// ComboBox of Arrive Service.
	private String[] arriveService = { "(Service)", "Train", "Tram", "Bus" };
	// ComboBox of Arrive Line.
	private String[] arriveLine = { "(Line)" };
	// ComboBox of arriving Train line.
	private String[] arriveTrainLine = { "(Line)", "Belgrave", "Lilydale" };
	// ComboBox of arriving Tram line.
	private String[] arriveTramLine = { "(Line)", "109" };
	// ComboBox of Arrive Station Default.
	private String[] arriveStationDefault = { "(Station/Stop)" };
	// ComboBox of Arrive Station.
	private String[] arriveStation = {};
	// ComboBox of Departing Day.
	private String[] departingDay = { "(Day)", "1", "2", "3", "4", "5", "6",
			"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
			"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
			"29", "30", "31" };
	// ComboBox of Depart Month Year.
	private String[] departingMonthYear = { "(Month/Year)", "01/2012",
			"02/2012", "03/2012", "04/2012", "05/2012", "06/2012", "07/2012",
			"08/2012", "09/2012", "10/2012", "11/2012", "12/2012" };
	// ComboBox of Departing Hour.
	private String[] departingHour = { "(Hour)", "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
			"18", "19", "20", "21", "22", "23" };
	// ComboBox of Departing Minute.
	private String[] departingMinute = { "(Minute)", "00", "05", "10", "15",
			"20", "25", "30", "35", "40", "45", "50", "55" };
	// Set default model for combo boxes.
	private DefaultComboBoxModel modelLine = new DefaultComboBoxModel();
	private DefaultComboBoxModel modelStation = new DefaultComboBoxModel();
	// Create combo boxes to the UIPanelRightNorth.
	JComboBox comboBoxDepartService = new JComboBox(departService);
	JComboBox comboBoxDepartLine = new JComboBox(departLine);
	JComboBox comboBoxDepartStation = new JComboBox(departStationDefault);
	JComboBox comboBoxArriveService = new JComboBox(arriveService);
	JComboBox comboBoxArriveLine = new JComboBox(arriveLine);
	JComboBox comboBoxArriveStation = new JComboBox(arriveStationDefault);
	JComboBox comboBoxDepartingDay = new JComboBox(departingDay);
	JComboBox comboBoxDepartingMonthYear = new JComboBox(departingMonthYear);
	JComboBox comboBoxDepartingHour = new JComboBox(departingHour);
	JComboBox comboBoxDepartingMinute = new JComboBox(departingMinute);
	JLabel empty = new JLabel(" ");
	// Get current value.
	String departStationValue = (String) comboBoxDepartStation
			.getSelectedItem();
	String arriveStationValue = (String) comboBoxArriveStation
			.getSelectedItem();
	String dayValue = (String) comboBoxDepartingDay.getSelectedItem();
	String monthYearValue = (String) comboBoxDepartingMonthYear
			.getSelectedItem();

	public UIPanelRightNorth() {
		// Set grid layout, 4 rows, 3 columns, horizontal gap 3 and vertical gap
		// 5 between components.
		setLayout(new GridLayout(4, 3, 3, 5));
		// Add combo boxes to the UIPanelRightNorth.
		// First row.
		add(comboBoxDepartService);
		add(comboBoxDepartLine);
		add(comboBoxDepartStation);
		// Second row.
		add(comboBoxArriveService);
		add(comboBoxArriveLine);
		add(comboBoxArriveStation);
		// Third row.
		add(comboBoxDepartingDay);
		add(comboBoxDepartingMonthYear);
		add(empty);
		// Fourth row.
		add(comboBoxDepartingHour);
		add(comboBoxDepartingMinute);

		/**
		 ** Create and register listener.
		 **/
		// Service from train, tram or bus.
		comboBoxDepartService.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				JComboBox comboBoxDepartService = (JComboBox) evt.getSource();
				String elementDepartService = (String) comboBoxDepartService
						.getSelectedItem();
				try {
					if (elementDepartService.equals("Train")) {
						// modelLine.removeAllElements();
						comboBoxDepartLine.removeAllItems();
						for (int i = 0; i < departTrainLine.length; i++) {
							// modelLine.(departTrainLine[i]);
							comboBoxDepartLine.addItem(departTrainLine[i]);
						}
					} else if (elementDepartService.equals("Tram")) {
						// modelLine.removeAllElements();
						comboBoxDepartLine.removeAllItems();
						for (int i = 0; i < departTramLine.length; i++) {
							comboBoxDepartLine.addItem(departTramLine[i]);
						}
					}
				} catch (Exception exc) {
					System.out
							.println("Somthing wrong in comboBoxDepartService.");
				}
			}
		});
		// Service to train, tram or bus.
		comboBoxArriveService.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				JComboBox comboBoxArriveService = (JComboBox) evt.getSource();
				String elementArriveService = (String) comboBoxArriveService
						.getSelectedItem();
				try {
					if (elementArriveService.equals("Train")) {
						comboBoxArriveLine.removeAllItems();
						for (int i = 0; i < departTrainLine.length; i++) {
							comboBoxArriveLine.addItem(arriveTrainLine[i]);
						}
					} else if (elementArriveService.equals("Tram")) {
						comboBoxArriveLine.removeAllItems();
						for (int i = 0; i < departTramLine.length; i++) {
							comboBoxArriveLine.addItem(arriveTramLine[i]);
						}
					}
				} catch (Exception exc) {
					System.out
							.println("Somthing wrong in comboBoxArriveService.");
				}
			}
		});
		// Line to train, tram or bus.
		comboBoxDepartLine.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				try {
					if (comboBoxDepartLine.getSelectedItem() == "Belgrave") {
						CSVReader reader = new CSVReader(new FileReader(
								"data/train/Belgrave-Belgrave.csv"));
						String[] line;
						boolean hasRead = false;
						ArrayList<String> stationNames = new ArrayList<String>();
						while ((line = reader.readNext()) != null) {
							if (line[0].startsWith("[") && hasRead == false) {
								hasRead = true;
								continue;
							}
							if (line[0].startsWith("[") && hasRead == true) {
								break;
							}
							if (line[0].contains("DEP")) {
								line[0] = line[0].replace("DEP", "").trim();
							}
							if (line[0].contains("ARR")) {
								continue;
							}
							// Test if all values are correct.
							// System.out.println(line[0]);
							stationNames.add(line[0]);
						}
						Iterator<String> stationNamesIterator = stationNames
								.iterator();
						departStation = new String[stationNames.size()];
						int departStationIndex = 0;
						while (stationNamesIterator.hasNext()) {
							departStation[departStationIndex++] = stationNamesIterator
									.next();
						}
						for (int i = 0; i < departStationIndex - 1; i++) {
							for (int j = i + 1; j < departStationIndex; j++) {
								if (departStation[i].equals(departStation[j])) {
									if (j == departStationIndex - 1) {
										departStationIndex--;
									} else {
										for (int k = j; k < departStationIndex - 1; k++) {
											departStation[k] = departStation[k + 1];
										}
										departStationIndex--;
									}
								}
							}
						}
						comboBoxDepartStation.removeAllItems();
						comboBoxDepartStation.addItem("(Station/Stop)");
						for (int i = 0; i < departStationIndex; i++) {
							comboBoxDepartStation.addItem(departStation[i]);
						}
					} else if (comboBoxDepartLine.getSelectedItem() == "Lilydale") {
						CSVReader reader = new CSVReader(new FileReader(
								"data/train/Lilydale-Lilydale.csv"));
						String[] line;
						boolean hasRead = false;
						ArrayList<String> stationNames = new ArrayList<String>();
						while ((line = reader.readNext()) != null) {
							if (line[0].startsWith("[") && hasRead == false) {
								hasRead = true;
								continue;
							}
							if (line[0].startsWith("[") && hasRead == true) {
								break;
							}
							if (line[0].contains("DEP")) {
								line[0] = line[0].replace("DEP", "").trim();
							}
							if (line[0].contains("ARR")) {
								continue;
							}
							// Test if all values are correct.
							// System.out.println(line[0]);
							stationNames.add(line[0]);
						}
						Iterator<String> stationNamesIterator = stationNames
								.iterator();
						departStation = new String[stationNames.size()];
						int departStationIndex = 0;
						while (stationNamesIterator.hasNext()) {
							departStation[departStationIndex++] = stationNamesIterator
									.next();
						}
						for (int i = 0; i < departStationIndex - 1; i++) {
							for (int j = i + 1; j < departStationIndex; j++) {
								if (departStation[i].equals(departStation[j])) {
									if (j == departStationIndex - 1) {
										departStationIndex--;
									} else {
										for (int k = j; k < departStationIndex - 1; k++) {
											departStation[k] = departStation[k + 1];
										}
										departStationIndex--;
									}
								}
							}
						}
						comboBoxDepartStation.removeAllItems();
						comboBoxDepartStation.addItem("(Station/Stop)");
						for (int i = 0; i < departStationIndex; i++) {
							comboBoxDepartStation.addItem(departStation[i]);
						}
					} else if (comboBoxDepartLine.getSelectedItem() == "109") {
						CSVReader reader = new CSVReader(new FileReader(
								"data/tram/109-Box_Hill.csv"));
						String[] line;
						boolean hasRead = false;
						ArrayList<String> stationNames = new ArrayList<String>();
						while ((line = reader.readNext()) != null) {
							if (line[0].startsWith("[") && hasRead == false) {
								hasRead = true;
								continue;
							}
							if (line[0].startsWith("[") && hasRead == true) {
								break;
							}
							if (line[0].contains("DEP")) {
								line[0] = line[0].replace("DEP", "").trim();
							}
							if (line[0].contains("ARR")) {
								continue;
							}
							// Test if all values are correct.
							// System.out.println(line[0]);
							stationNames.add(line[0]);
						}
						Iterator<String> stationNamesIterator = stationNames
								.iterator();
						departStation = new String[stationNames.size()];
						int departStationIndex = 0;
						while (stationNamesIterator.hasNext()) {
							departStation[departStationIndex++] = stationNamesIterator
									.next();
						}
						for (int i = 0; i < departStationIndex - 1; i++) {
							for (int j = i + 1; j < departStationIndex; j++) {
								if (departStation[i].equals(departStation[j])) {
									if (j == departStationIndex - 1) {
										departStationIndex--;
									} else {
										for (int k = j; k < departStationIndex - 1; k++) {
											departStation[k] = departStation[k + 1];
										}
										departStationIndex--;
									}
								}
							}
						}
						comboBoxDepartStation.removeAllItems();
						comboBoxDepartStation.addItem("(Station/Stop)");
						for (int i = 0; i < departStationIndex; i++) {
							comboBoxDepartStation.addItem(departStation[i]);
						}
					}
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		// Line from train, tram or bus.
		comboBoxArriveLine.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				try {
					if (comboBoxArriveLine.getSelectedItem() == "Belgrave") {
						CSVReader reader = new CSVReader(new FileReader(
								"data/train/Belgrave-Belgrave.csv"));
						String[] line;
						boolean hasRead = false;
						ArrayList<String> stationNames = new ArrayList<String>();
						while ((line = reader.readNext()) != null) {
							if (line[0].startsWith("[") && hasRead == false) {
								hasRead = true;
								continue;
							}
							if (line[0].startsWith("[") && hasRead == true) {
								break;
							}
							if (line[0].contains("DEP")) {
								line[0] = line[0].replace("DEP", "").trim();
							}
							if (line[0].contains("ARR")) {
								continue;
							}
							// Test if all values are correct.
							// System.out.println(line[0]);
							stationNames.add(line[0]);
						}
						Iterator<String> stationNamesIterator = stationNames
								.iterator();
						arriveStation = new String[stationNames.size()];
						int arriveStationIndex = 0;
						while (stationNamesIterator.hasNext()) {
							arriveStation[arriveStationIndex++] = stationNamesIterator
									.next();
						}
						for (int i = 0; i < arriveStationIndex - 1; i++) {
							for (int j = i + 1; j < arriveStationIndex; j++) {
								if (arriveStation[i].equals(arriveStation[j])) {
									if (j == arriveStationIndex - 1) {
										arriveStationIndex--;
									} else {
										for (int k = j; k < arriveStationIndex - 1; k++) {
											arriveStation[k] = arriveStation[k + 1];
										}
										arriveStationIndex--;
									}
								}
							}
						}
						comboBoxArriveStation.removeAllItems();
						comboBoxArriveStation.addItem("(Station/Stop)");
						for (int i = 0; i < arriveStationIndex; i++) {
							comboBoxArriveStation.addItem(arriveStation[i]);
						}
					} else if (comboBoxArriveLine.getSelectedItem() == "Lilydale") {
						CSVReader reader = new CSVReader(new FileReader(
								"data/train/Lilydale-Lilydale.csv"));
						String[] line;
						boolean hasRead = false;
						ArrayList<String> stationNames = new ArrayList<String>();
						while ((line = reader.readNext()) != null) {
							if (line[0].startsWith("[") && hasRead == false) {
								hasRead = true;
								continue;
							}
							if (line[0].startsWith("[") && hasRead == true) {
								break;
							}
							if (line[0].contains("DEP")) {
								line[0] = line[0].replace("DEP", "").trim();
							}
							if (line[0].contains("ARR")) {
								continue;
							}
							// Test if all values are correct.
							// System.out.println(line[0]);
							stationNames.add(line[0]);
						}
						Iterator<String> stationNamesIterator = stationNames
								.iterator();
						arriveStation = new String[stationNames.size()];
						int arriveStationIndex = 0;
						while (stationNamesIterator.hasNext()) {
							arriveStation[arriveStationIndex++] = stationNamesIterator
									.next();
						}
						for (int i = 0; i < arriveStationIndex - 1; i++) {
							for (int j = i + 1; j < arriveStationIndex; j++) {
								if (arriveStation[i].equals(arriveStation[j])) {
									if (j == arriveStationIndex - 1) {
										arriveStationIndex--;
									} else {
										for (int k = j; k < arriveStationIndex - 1; k++) {
											arriveStation[k] = arriveStation[k + 1];
										}
										arriveStationIndex--;
									}
								}
							}
						}
						comboBoxArriveStation.removeAllItems();
						comboBoxArriveStation.addItem("(Station/Stop)");
						for (int i = 0; i < arriveStationIndex; i++) {
							comboBoxArriveStation.addItem(arriveStation[i]);
						}
					} else if (comboBoxArriveLine.getSelectedItem() == "109") {
						CSVReader reader = new CSVReader(new FileReader(
								"data/tram/109-Box_Hill.csv"));
						String[] line;
						boolean hasRead = false;
						ArrayList<String> stationNames = new ArrayList<String>();
						while ((line = reader.readNext()) != null) {
							if (line[0].startsWith("[") && hasRead == false) {
								hasRead = true;
								continue;
							}
							if (line[0].startsWith("[") && hasRead == true) {
								break;
							}
							if (line[0].contains("DEP")) {
								line[0] = line[0].replace("DEP", "").trim();
							}
							if (line[0].contains("ARR")) {
								continue;
							}
							// Test if all values are correct.
							// System.out.println(line[0]);
							stationNames.add(line[0]);
						}
						Iterator<String> stationNamesIterator = stationNames
								.iterator();
						arriveStation = new String[stationNames.size()];
						int arriveStationIndex = 0;
						while (stationNamesIterator.hasNext()) {
							arriveStation[arriveStationIndex++] = stationNamesIterator
									.next();
						}
						for (int i = 0; i < arriveStationIndex - 1; i++) {
							for (int j = i + 1; j < arriveStationIndex; j++) {
								if (arriveStation[i].equals(arriveStation[j])) {
									if (j == arriveStationIndex - 1) {
										arriveStationIndex--;
									} else {
										for (int k = j; k < arriveStationIndex - 1; k++) {
											arriveStation[k] = arriveStation[k + 1];
										}
										arriveStationIndex--;
									}
								}
							}
						}
						comboBoxArriveStation.removeAllItems();
						comboBoxArriveStation.addItem("(Station/Stop)");
						for (int i = 0; i < arriveStationIndex; i++) {
							comboBoxArriveStation.addItem(arriveStation[i]);
						}
					}
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	public void checkValidate() {
		if ((monthYearValue.equals("04/2012")
				|| monthYearValue.equals("06/2012")
				|| monthYearValue.equals("09/2012") || monthYearValue
					.equals("11/2012")) && dayValue.equals("31")) {
			checkError = 1;
			System.out.println("E-01 Your date input is incorrect.");
		}
		if (monthYearValue.equals("02/2012")
				&& (dayValue.equals("30") || dayValue.equals("31"))) {
			checkError = 2;
			System.out.println("E-01 Your date input is incorrect.");
		}
		if (departStationValue.equals(arriveStationValue)) {
			checkError = 3;
			System.out.println("E-02 Your destination input is incorrect.");
		}
	}
}

class UIPanelCenter extends JPanel {
	UIPanelSouth panelSouth = new UIPanelSouth();
	JButton searchButton = new JButton("Search");

	public UIPanelCenter() {
		add(searchButton);
		// Register listeners with the search button.
		searchButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		searchButton.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("Checking the button when it is clicked.");
				panelSouth.outputBox.setText("Hello");
				panelSouth.outputBox.setName("Hello");
				setName("Hello");
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}
}

class UIPanelSouth extends JPanel {
	JTextArea outputBox = new JTextArea(10, 50);
	JScrollPane scrollPane = new JScrollPane(outputBox);

	public UIPanelSouth() {
		// Set flow layout, aligned right with horizontal gap 100 and vertical
		// gap 30 between components.
		setLayout(new FlowLayout(FlowLayout.CENTER, 100, 30));
		add(outputBox);
		// Add scroll bars for the text area.
		JScrollPane scrollPane = new JScrollPane(outputBox);
		add(scrollPane, BorderLayout.CENTER);
		// Set font in the text field.
		outputBox.setFont(new Font("SansSerif", Font.ITALIC, 14));
		// Set the lineWrap and wrapStyleWord true for the text area.
		outputBox.setLineWrap(true);
		outputBox.setWrapStyleWord(true);
		outputBox.setEditable(true);
	}

	public void setOutPutBox(String text) {
		// outputBox.setText(text);
	}
}